package Relays

import (
	"errors"
	"github.com/yuin/gopher-lua"
	"reflect"
)

type LuaManager struct {
	Rule lua.P
}

func (luaManager *LuaManager) New(importNm string)(*lua.LState) {
	setChatMessage := lua.NewState()
	setChatMessage.DoFile("D://gospace/src/Volta/script/"+ importNm +".lua")
	return setChatMessage
}

func (luaManager *LuaManager) Call(luaState *lua.LState,funcNm string,param interface{})(interface{},error){
	fn := luaState.GetGlobal(funcNm)
	luaManager.Rule.Fn = fn
	paramType := reflect.TypeOf(param).String()
	var err error = nil
	if paramType == "string" {
		err = luaState.CallByParam(luaManager.Rule,lua.LString(param.(string)))
	}else if paramType == "int" {
		err = luaState.CallByParam(luaManager.Rule,lua.LNumber(param.(float32)))
	}
	if err != nil {
		return "",errors.New("call error")
	}
	return luaState.Get(-1),nil
}
